package service;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.mybatis.mapper.CheckinfoMapper;
import com.mybatis.po.Checkinfo;

@Service
public class CheckService {
	@Autowired
	private CheckinfoMapper checkinfomapper;
	
	public String getimage(List<MultipartFile> photo) {
		String tmpimg="";
		//String path="D:/java/eclipse-workspace/sct/src/main/webapp/assets/img";
		//String path="";
		String path = CheckService.class.getResource("").getPath()+"../../../upload";
	      File f=new File(path);
	      if(!f.exists()){
	          f.mkdirs();//创建存储路径
	      }
		for(int i=0;i<photo.size();i++) {
			MultipartFile file=photo.get(i);
			String fileName=file.getOriginalFilename();
			//String fileName=session.getAttribute("Uid").toString()+".jpg";
			File targetFile=new File(path,fileName);
			tmpimg=path+"/"+fileName;
			try {
				file.transferTo(targetFile);
			}catch(Exception e) {
				e.printStackTrace();
			}
	      }
		return tmpimg;
	}
	public String sistance(String path) {
		String ans="";
		//String path1 = CheckService.class.getResource("").getPath();
		try {
			path=path.substring(1);
	        //String[] args = new String[] { "python", "D:\\java\\eclipse-workspace\\sct\\src\\main\\java\\service\\face.py", path };
			String[] args = new String[] { "python", CheckService.class.getResource("").getPath().substring(1)+"face.py", path };  
			Process proc = Runtime.getRuntime().exec(args);// 执行py文件

	          //用输入输出流来截取结果
	          BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), "GBK"));
	          String line = null;
	          while ((line = in.readLine()) != null) {
	              System.out.println(line);
	              ans+=line;
	          }
	          in.close();
	          proc.waitFor();
	      } catch (IOException e) {
	          e.printStackTrace();
	      } catch (InterruptedException e) {
	          e.printStackTrace();
	      }
		return ans;
	}
	public void add(String Uid,String Uname,String Cid,int ck) {
		Checkinfo c1=new Checkinfo();
	    c1.setUid(Uid);
	    c1.setUname(Uname);
	    c1.setCid(Cid);
	    c1.setCheck1(ck);
	    checkinfomapper.addCheck(c1);
	}
}
